package com.mingqijia.gassafety.authority.security.facade;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mingqijia.gassafety.authority.api.req.role.CreateRoleReqDTO;
import com.mingqijia.gassafety.authority.api.req.role.QueryRoleReqDTO;
import com.mingqijia.gassafety.authority.api.req.role.UpdateRoleReqDTO;
import com.mingqijia.gassafety.authority.api.resp.role.RoleInfoRespDTO;
import com.mingqijia.gassafety.authority.api.web.facade.RoleManageFacade;
import com.mingqijia.gassafety.authority.dal.command.RemoveRoleCommand;
import com.mingqijia.gassafety.authority.dal.command.UpdateRoleCommand;
import com.mingqijia.gassafety.authority.dal.handler.RoleTranslator;
import com.mingqijia.gassafety.authority.dal.provider.RoleProvider;
import com.mingqijia.gassafety.db.entity.auth.RoleEntry;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.enums.RunningState;
import com.mingqijia.gassafety.shared.facade.BaseFacade;
import com.mingqijia.gassafety.shared.handler.GlobalAuthenticationException;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * REST接口
 * @author Damon S.
 * @version v0.2
 * @date 2021-06-19
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class RoleManageFacadeImpl extends BaseFacade implements RoleManageFacade {

    private final RoleProvider provider;
    private final RoleTranslator translator;


    @Override
    public RoleInfoRespDTO findRoleById(String roleIdOrName) {
        RoleEntry entry = provider.findRoleByIdOrName(roleIdOrName);
        if (ObjectUtils.isEmpty(entry)) {
            return null;
        }
        return translator.toRespDTO(entry);
    }

    @Override
    public PageUtil<RoleInfoRespDTO> findRole(QueryRoleReqDTO req) {
        Page<RoleEntry> results = provider.find(translator.toCommand(req));
        return PageUtil.init(results, translator.toRespDTO(results.getRecords()));
    }

    @Override
    public String createRole(CreateRoleReqDTO req) {
        return provider.create(translator.toCommand(req));
    }

    @Override
    public void updateRole(UpdateRoleReqDTO req) {
        if (!provider.update(translator.toCommand(req))) {
            throw new GlobalAuthenticationException("操作失败");
        }
    }

    @Override
    public void removeRole(String[] roleId) {
        RemoveRoleCommand command = RemoveRoleCommand.builder()
                .userIds(Arrays.asList(roleId)).build();
        if (!provider.remove(command)) {
            throw new GlobalAuthenticationException("操作失败");
        }
    }

    @Override
    public void switchRole(String roleId, Integer state) {
        if (state > Constants.INT_ONE || state < Constants.INT_ZERO) {
            throw new GlobalAuthenticationException("请求参数不合法");
        }
        UpdateRoleCommand command = UpdateRoleCommand.builder()
                .id(roleId).state(RunningState.parse(state)).build();
        if (!provider.update(command)) {
            throw new GlobalAuthenticationException("操作失败");
        }
    }
}
